#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int hIndex(vector<int>& citations) {
        if (citations.size() == 0) {
            return 0;
        } else if (citations.size() == 1) {
            return min(1, citations[0]);
        }
        int ans = 0;
        sort(citations.begin(), citations.end());
        int i1 = 0;
        int i2 = 1;
        ans = min(2, citations[i1]);
        while (i1 != citations.size() - 1 || i2 != citations.size() - 1) {
            if (i2 == citations.size() - 1) {
                i1++;
                ans = max(ans, min(i2 - i1 + 1, citations[i1]));
                continue;
            }
            bool head = min(i2 - i1 + 2, citations[i1]) >= min(i2 - i1, citations[i1 + 1]);
            if (head) {
                i2++;
                ans = max(ans, min(i2 - i1 + 1, citations[i1]));
            } else {
                i1++;
                ans = max(ans, min(i2 - i1 + 1, citations[i1]));
            }
        }
        return ans;
    }
};

int main() {
    vector<int> input = {3, 0, 6, 1, 5};
    Solution solution;
    solution.hIndex(input);
    return 0;
}